<template>
  <div v-show="$route.name == 'inner-purchase-list'">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>内部采购记录</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <div style="margin-bottom: 20px;">内部采购记录</div>
      <!-- 搜索与添加区域 -->
      <el-row class="query" v-if="groupid === 8 || groupid === 3 || groupid === 4">
        <el-col :span="24">
          <el-button type="success" @click="addInnerPurchase" style="margin-bottom: 10px">内部采购</el-button>
        </el-col>
      </el-row>
      <el-row class="query" v-else>
        <el-col :span="24">
          <el-select v-model="searchList.base" popper-class="dropdownbox" style="margin-left: 10px;width:200px" placeholder="请选择门店">
            <el-option v-for="item in storeList" :label="item.basename"
              :value="item.base" :key="item.base">
              <span style="float: left">{{ item.basename }}</span>
            </el-option>
          </el-select>
          <el-button type="primary" @click="search" style="margin-left: 5px">查询</el-button>
          <el-button style="margin-left: 5px"  @click="reset">重置</el-button>
        </el-col>
      </el-row>

      <el-table
        border
        v-loading="loading"
        :data="innerPurchaseData"
        style="margin-top: 10px;">
        <el-table-column label="采购日期" prop="purchase_time" min-width="110" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.purchase_time ? new Date(scope.row.purchase_time).toLocaleDateString().split('/').join('-'): '' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="采购单号" prop="sell_number" min-width="110" align="center"></el-table-column>
        <el-table-column label="门店" prop="basename" min-width="80" align="center"></el-table-column>
        <el-table-column label="采购状态" prop="status" min-width="80" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.status === 1">已完成</span>
            <span v-else-if="scope.row.status === 2">运营审核中</span>
            <span v-else-if="scope.row.status === 3">仓管审核中</span>
            <span v-else-if="scope.row.status === 4">驳回</span>
            <span v-else-if="scope.row.status === 5">未提交</span>
          </template>
        </el-table-column>
        <el-table-column label="运营审批时间" prop="operate_time" min-width="110" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.operate_time ? new Date(scope.row.operate_time).toLocaleDateString().split('/').join('-') : '' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="审批通过时间" prop="confirm_time" min-width="110" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.confirm_time ? new Date(scope.row.confirm_time).toLocaleDateString().split('/').join('-') : '' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="160" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="clickButton(scope.row.id, 1)" style="margin-right: 5px;">详情</el-button>
            <el-button size="mini" type="warning" @click="clickButton(scope.row.id, 2)" v-if="scope.row.status > 1 && groupid === 8">编辑</el-button>
          </template>
        </el-table-column>
       
      </el-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pager.currPage"
        :page-sizes="[1, 3, 5, 10]"
        :page-size="pager.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pager.totalCount">
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      loading: false,
      pager: {
        currPage: 1,
        pageSize: 10,
        totalCount: 0
      },
      base: [],
      groupid: '',
      innerPurchaseData: [],
      storeList: [],
      searchList: {
        base: ''
      }
    }
  },
  mounted() {
    this.getUserInfo()
  },
  methods: {
    // 获取门店
    async getStoreList() {
      const { data: result } = await this.$http.get('/store/all')
      if (result.status !== 200) return this.$message.error(result.msg)
      if (this.groupid === 3 || this.groupid === 4) {
        this.storeList = result.data.filter((el) => {
          return this.base.includes(el.base)
        })
      } else {
        this.storeList = result.data
      }
    },
    // 获取用户信息
    async getUserInfo() {
      const { data: result } = await this.$http.get('/user/' + localStorage.userid)
      if (result.status !== 200) return this.$message.error(result.msg)
      this.groupid = result.data.groupid * 1
      if (this.groupid === 8) {
        this.base = result.data.base ? [result.data.base * 1] : [0]
      } else if (this.groupid === 3 || this.groupid === 4) {
        this.base = result.data.operate_store ? result.data.operate_store.split(',') : [0]
        this.base = this.base.map((el) => {
          return parseInt(el)
        })
      }
      this.getStoreList()
      this.getInnerList()
    },
    async getInnerList() {
      this.loading = true
      var params = {}
      if (this.groupid === 8) {
        params = {
          page: this.pager.currPage,
          pageSize: this.pager.pageSize,
          base: this.base,
          status: [1, 2, 3, 4, 5]
        }
      } else {
        params = {
          page: this.pager.currPage,
          pageSize: this.pager.pageSize,
          base: this.searchList.base ? [this.searchList.base] : this.base,
          status: [1, 2, 3, 4, 5]
        }
      }
      const { data: result } = await this.$http.get('/store-inner', { params: params })
      if (result.status !== 200) {
        this.loading = false
        this.innerPurchaseData = '暂无数据'
        return this.$message.error(result.msg)
      }
      this.loading = false
      this.innerPurchaseData = result.data
      this.pager = result.pager
    },
    handleSizeChange(pageSize) {
      this.pager.pageSize = pageSize
      this.getInnerList()
    },
    handleCurrentChange(currentPage) {
      this.pager.currPage = currentPage
      this.getInnerList()
    },
    addInnerPurchase() {
      this.$router.push({ 'name': 'inner-purchase-save' })
    },
    clickButton(id, type) {
      if (type ===1 ) {
        this.$router.push({
          name: 'inner-purchase-detail',
          params: { id: id }
        })
      } else if (type === 2) {
        this.$router.push({
          name: 'inner-purchase-update',
          params: { id: id }
        })
      }
    },
    reset() {
      this.searchList = {
        base: ''
      },
      this.innerPurchaseData = []
      this.pager.currPage = 1
      this.getInnerList()
    },
    search() {
      this.innerPurchaseData = []
      this.pager.currPage = 1
      this.getInnerList()
    }
  }
}
</script>

<style lang="less">
  .query {
    margin-bottom: 20px;
  }
</style>